package com.xuptGurantee.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuptGurantee.system.common.R;
import com.xuptGurantee.system.dto.ActivityDto;
import com.xuptGurantee.system.entity.Activity;
import com.xuptGurantee.system.entity.Volunteer;
import com.xuptGurantee.system.service.ActivityDtoService;
import com.xuptGurantee.system.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    ActivityService activityService;

    @Resource
    ActivityDtoService activityDtoService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name ,String handlerId) {
        log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        long id = Long.parseLong(handlerId);
        //构造条件构造器
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name), Activity::getActivityTitle, name);
        //添加排序条件
        queryWrapper.orderByDesc(Activity::getActivityDate);
        queryWrapper.eq(Activity::getPublisherId,id);
        //执行查询
        activityService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @PostMapping("/add")
    public R<String> addActivity(HttpServletRequest request, @RequestBody Activity activity) {
        log.info("新增活动，活动信息：{}", activity.toString());
        activity.setActivityStatus(1);
        log.info("{}", activity.getActivityDate());
        activityService.save(activity);

        return R.success("新增活动成功");
    }

    @PutMapping("/modifyStatus")
    public R<String> modifyStatus(HttpServletRequest request, @RequestBody Activity activity) {
        activityService.updateById(activity);
        return R.success("活动状态修改成功");
    }

    @PutMapping("/modify")
    public R<String> modifyInfo(HttpServletRequest request, @RequestBody Activity activity) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getId, activity.getId());
        activityService.updateById(activity);
        return R.success("活动信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Activity> getById(@PathVariable Long id) {
        log.info("根据id查询活动信息...");
        Activity activity = activityService.getById(id);
        if (activity != null) {
            return R.success(activity);
        }
        return R.error("没有查询到对应活动信息");
    }

    @DeleteMapping("/{id}")
    public R<String> deleteById(@PathVariable Long id) {
        log.info("根据id查询活动信息...");
        if (activityService.removeById(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @PostMapping("/getAct")
    public R<List<ActivityDto>> getVolActList(@RequestBody ActivityDto activityDto) {
        List<ActivityDto> activityDtoList = activityDtoService.getActDtoList(activityDto);
        return R.success(activityDtoList);
    }

    @GetMapping("/publishAct")
    public R<List<Activity>> pubList() {
        List<Activity> pubList = activityService.getPubList();
        return R.success(pubList);
    }


}
